#!/usr/bin/env node

const router = require('koa-router')(),
      users = require('./../models/users.js'),
      { JWT_KEY } = require('../lib/constant'),
      jwt = require('jsonwebtoken'),
      _ = require('lodash'),
      log = console.log;

router.prefix('/api/users');


router.post('/login', async(ctx, next) => {
  let { email, password } = ctx.request.body;

  let res = await users.login(email, password);
  if(res.code === 0){
    let token = await jwt.sign(_.toPlainObject(res.data), JWT_KEY);
    res.data = token;
  }

  ctx.body = res;
});
module.exports= router;

